<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * CustomerAssess_model
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class CustomerAssess_model extends MY_Model{
    public function __construct(){
            parent::__construct();
            $this->table_name = 'book_customerassess';
    }
    
    public function updateStatus($params){
            $sql    = "UPDATE {$this->table_name} SET status = {$params['status']} WHERE customerID = '".$params['customerID']."'";
            $this->db->query($sql);
            if($this->db->affected_rows()==0){
                die($this->lang->line("contactAdmin").'->CustomerAssess_model/updateStatus->18');
            }
            return $this->db->affected_rows();
    }
    
    public function updateInfo($data,$where=NULL){
            if($where){
                foreach($where as $keys => $vals){
                    $this->db->where($keys, $vals);
                }
            }
            $this->db->update($this->table_name, $data); 
//            echo $this->db->last_query();exit;
    }

    public function init_data($right){
		return array(
                               array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' => 'name',
						'label' => $this->lang->line('name'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
				array(
						'name' => 'phone',
						'label' => $this->lang->line('phone'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'real_fare',
						'label' => $this->lang->line('real_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'ngay_dau_bay',
						'label' => $this->lang->line('ngay_dau_bay'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'status',
						'label' => $this->lang->line('status'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' 	=> 'button',
						'label' => $this->lang->line('button'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("*")
		->from($this->table_name)
                ->where('ngay_dau_bay >= DATE_ADD(CURDATE(), INTERVAL +2 DAY)')
                ->where('(phone_callstatus = 3 OR phone_callstatus = 0)');
		//->unset_column('id');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();

		foreach($datatables['aaData'] as $item){
                        $phone      = json_decode($item['phone']);
                        $booker     = $this->getInfoUserByParams(array('key'=>'id','value'=>$item['booker']));

			$ouput['aaData'][] = array(
                            $item['id'],
                            $item['name'],
                            $phone[0],
                            $booker->username,
                            $this->bookinglib->my_number_format($item['real_fare'],2, ',', ','),
                            date("d-m-Y",strtotime($item['ngay_dau_bay'])),
                            ($item['status']==1)?$this->lang->line('calling'):'---',
                            $this->add_button_public($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}
        
}